<?php


function listAllCategory($parent_id = 0,$space = ' ', $trees = NULL) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM cat_document WHERE  cat_parent = ".intval($parent_id)." ORDER BY cat_order");
    while($rs=$db->fetch_array($rsCat)) {
        if($rs['cat_parent']==0) {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date'],"cat_link"=>$rs['cat_link']);
        }else {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$space.$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date'],"cat_link"=>$rs['cat_link']);
        }
        $trees = listAllCategory($rs['cat_id'],$space.'----',$trees);
    }
    return $trees;
}
function getDocsByCat($cat_id)
{
    global $db;
    $rsDocs=$db->query("SELECT id,cat_id,filename,realname,title FROM document WHERE cat_id=".intval($cat_id)." ORDER BY id DESC lIMIT 10");
    return $rsDocs;
}
function getDocByCatLink($catLink)
{
 global $db;
 $catId = getCatIdByCatLink($catLink);
 $rsDoc=$db->query("SELECT * FROM document WHERE cat_id=".intval($catId)." ORDER BY id DESC");
 return $rsDoc;
}
function getCatIdByCatLink($catLink='') {
    global $db;
    if($catLink) {
        $rs = $db->query_first("SELECT cat_id FROM cat_document WHERE cat_link = '".mysql_real_escape_string($catLink)."'");
        if($rs['cat_id']) {
            return $rs['cat_id'];
        }else {
            return false;
        }
    }else {
        return false;
    }
}
function getCatInfoByCatLink($catLink='') {
    global $db;
    $catId=getCatIdByCatLink($catLink);
    if($catId==false) {
        $catId = 1;
    }   
      $rs = $db->query_first("SELECT cat_title FROM cat_document WHERE cat_id =".intval($catId));
    return $rs;         
}
function listAllDocs() {
    global $db,$CONFIG;
    if(intval($_REQUEST['page'])>0) {
        $page = $_REQUEST['page'];
    }else {
        $page = 1;
    }
    // neu' bam vao nut tim kiem
    if($_REQUEST['submitFrmNewsSearch']=='Search') {
        if($_REQUEST['keyword']!='') {
            $searchCondition = ' AND (n.realname LIKE \'%'.$_REQUEST['keyword'].'%\') ';
        }
        if(intval($_REQUEST['cat_id'])>0) {
            $arrCatId = getCatNewsIdToArray($_REQUEST['cat_id']);
            array_push($arrCatId,$_REQUEST['cat_id']);
            if(count($arrCatId)>0) {
                $strCatId = implode(',', $arrCatId);
                $searchCondition .= " AND c.cat_id IN ($strCatId)";
            }else {
                $searchCondition .= " AND c.cat_id =".$_REQUEST['cat_id'];
            }
        }
        //echo $searchCondition;
        $rsNews = $db->query("SELECT n.id FROM document n LEFT JOIN cat_document c ON n.cat_id = c.cat_id WHERE 1 $searchCondition ORDER BY n.create_date DESC");
        $totalRows =  $db->num_rows($rsNews);
        $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_news']);
        $start = (($page*$CONFIG ['rows_per_page_news'])-$CONFIG ['rows_per_page_news']);
        $limit = $CONFIG ['rows_per_page_news'];

        $rs = $db->query("SELECT n.id,n.cat_id,n.filename,n.realname,n.create_date,c.cat_title
                            FROM document n LEFT JOIN cat_document c ON n.cat_id = c.cat_id
                            WHERE 1 $searchCondition ORDER BY n.create_date DESC LIMIT $start,$limit");
        //chuyen ve` truong truoc neu' trang hien tai khong co' ban ghi nao (truong hop xoa)
        if($db->num_rows($rs)<1) {
            $page = $page - 1;
            if($page>1) {
                server_redirect(get_string_url()."&page=".$page);
            }

        }
        return array(
        "rs"=>$rs,
        "page"=>$page,
        "totalPages"=>$totalPages
        );
    }else {
        $rsNews = $db->query("SELECT n.id FROM document n LEFT JOIN cat_document c ON n.cat_id = c.cat_id WHERE 1 ORDER BY n.create_date DESC");
        $totalRows =  $db->num_rows($rsNews);
        $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_news']);
        $start = (($page*$CONFIG ['rows_per_page_news'])-$CONFIG ['rows_per_page_news']);
        $limit = $CONFIG ['rows_per_page_news'];

        $rs = $db->query("SELECT n.id,n.cat_id,n.filename,n.realname,n.create_date,c.cat_title
                            FROM document n LEFT JOIN cat_document c ON n.cat_id = c.cat_id
                            WHERE 1 ORDER BY n.create_date DESC LIMIT $start,$limit");
        //chuyen ve` truong truoc neu' trang hien tai khong co' ban ghi nao (truong hop xoa)
        if($db->num_rows($rs)<1) {
            $page = $page - 1;
            if($page>1) {
                redirect(get_string_url()."&page=".$page);
            }

        }
        return array(
        "rs"=>$rs,
        "page"=>$page,
        "totalPages"=>$totalPages
        );
    }
}


?>
